iT邦幫忙

DAY 19
0

x86 android 設備與外部硬體溝通研究系列 第 19

x86 android 設備與外部硬體溝通研究 - Android USB -2 (19/30)

  • 分享至 

  • xImage
  •  

今天示範的 code 經測試,能夠於下面裝置執行:

  1. Nexus 7 (2012) , Android 4.4.4
  2. Zenfone 6 , Android 4.3 / 4.4
  3. Sansung Note 3 , Android 4.3
  4. Sansung Note 3 LTE, Android 4.3

在以下裝置,安裝後嘗試啟動 USB 會導致 app crash

  1. Sony Xperia P , Android 4.1
  2. Sony Xperia GO , Android 4.1

今天我們要簡單試著用 android 控制 arduino

首先你需要去找這兩個檔案:

  1. usb-serial-for-android-v010.jar https://github.com/mik3y/usb-serial-for-android
  2. arduino-firmata.jar https://github.com/shokai/ArduinoFirmata-Android/

把上述兩個檔案放置於 ./lib 中,假設讀者已經依照昨天的步驟去設定 USB host mode 了

還記得我們最早的 sensor app project 嗎?今天的工作會由那個app開始,並僅限於 MainActivity.java 中作業

package com.example.test;

import java.io.*; 
import java.lang.*; //給啟動 USB IO 時 try/catch 用
import android.hardware.usb.*;  // 必要,拿來呼叫 USB hardware

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.MyTestLibrary.CountingSensorUpateRate;
import org.shokai.firmata.ArduinoFirmata;    // 今天主要用到這個
import org.shokai.firmata.ArduinoFirmataEventHandler; // 可省略

public class MainActivity extends ActionBarActivity{

	private boolean timerHasStarted = false ;
	private boolean ArduinoBlink = false ;
	private Button startB;
	private TestCounting CountDownTimer;
	private TextView AccelX;
	private TextView AccelY;
	private TextView AccelZ;
	private TextView OrienX;
	private TextView OrienY;
	private TextView OrienZ;
	private SensorActivity MySensor;
	private TextView TimeView;
	private TextView ElapsedView;
	private TextView CountAccel,CountOrien;
	private long timeElapsed;
	private long startTime = 10*1000; // 跑10秒鐘就好
	private long interval = 1*1000; // 1 sec. per time interval
	private CountingSensorUpateRate MyCountingSensorUpdateRate;
	

	private ArduinoFirmata arduino;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startB = (Button) this.findViewById(R.id.button1);
        startB.setOnClickListener(new Button_Listener(this));
        AccelX = (TextView) this.findViewById(R.id.AccelX);
        AccelY = (TextView) this.findViewById(R.id.AccelY);
        AccelZ = (TextView) this.findViewById(R.id.AccelZ);
        OrienX = (TextView) this.findViewById(R.id.OrienX);
        OrienY = (TextView) this.findViewById(R.id.OrienY);
        OrienZ = (TextView) this.findViewById(R.id.OrienZ);
    	TimeView= (TextView) this.findViewById(R.id.timer);
		ElapsedView= (TextView) this.findViewById(R.id.timeElapsed);
        CountAccel = (TextView) this.findViewById(R.id.UpateRate_Accel);
        CountOrien = (TextView) this.findViewById(R.id.UpateRate_Orien);
        CountDownTimer = new TestCounting (startTime,interval);
        MyCountingSensorUpdateRate = new CountingSensorUpateRate();

        MySensor = new SensorActivity();
        MySensor.onPause();

        this.arduino = new ArduinoFirmata(this);
        

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
   
    
    class Button_Listener implements OnClickListener {

    	  public Button_Listener(MainActivity activity) {

    	  }
    	  @Override
    	    public void onClick(View view) {
    	        if(!timerHasStarted){
    	        	CountDownTimer.start();
    	        	MySensor.onResume();

    	        	timerHasStarted = true;
    	        	startB.setText("RESET ME!");
    	        	// 試著啟動 arduino USB firmata connect !!!
    	    		try { 
    	    			arduino.connect();
    	    		} catch (IOException e) {
    	    			e.printStackTrace();
    	    		} catch (InterruptedException e) {
    	    			e.printStackTrace();
    	    		}
    	        }else{
    	        	CountDownTimer.cancel(); 
    	        	MySensor.onPause();
    	        	timerHasStarted = false;
    	        	startB.setText("Start Counting... 10 Sec.");

	    			//arduino.close();
    
    	    		
    	        }
    	    }
    	}
    public class TestCounting extends CountDownTimer{

		public TestCounting(long startTime, long interval) {
			super(startTime, interval);
		}
		@Override
		public void onFinish(){
			TimeView.setText("Time's Up");
			ElapsedView.setText("Time Elapsed:" + String.valueOf(startTime));
			CountAccel.setText("Upate Accel "+ MyCountingSensorUpdateRate.GetCounting("TYPE_ACCELEROMETER") +" Times per "+startTime+" ms");
			CountOrien.setText("Upate Orien "+  MyCountingSensorUpdateRate.GetCounting("TYPE_ORIENTATION") +" Times per "+startTime+" ms");
			MyCountingSensorUpdateRate.Clear();
			MySensor.onPause();
		}
		@Override
		public void onTick(long millisUntillFinished){
			TimeView.setText("Time remain : "+ millisUntillFinished);
			timeElapsed = startTime - millisUntillFinished;
			ElapsedView.setText("Time Elapsed : " + String.valueOf(timeElapsed));

			arduino.digitalWrite(13, ArduinoBlink); // on board LED ,我們藉由 arduino.digitalWrite 送出訊號給 arduino 第 13 PIN ,剛好是 LED 測試腳
			ArduinoBlink = ! ArduinoBlink; // 每 1 秒 tick 時變換明/滅,如此你可以看到一閃一滅的效果

		}
    	
    
    }
    public class SensorActivity implements SensorEventListener {
        private SensorManager mSensorManager;


		public SensorActivity() {
            mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
            //mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            //mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        }


		@SuppressWarnings("deprecation")
		protected void onResume() {
        	mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) , SensorManager.SENSOR_DELAY_FASTEST );
        	mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION) , SensorManager.SENSOR_DELAY_FASTEST );
        }

        protected void onPause() {
        	mSensorManager.unregisterListener(this);
        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }


		@SuppressWarnings("deprecation")
		public void onSensorChanged(SensorEvent event) {
			switch(event.sensor.getType()){
			case Sensor.TYPE_ACCELEROMETER :
				MyCountingSensorUpdateRate.CountAs("TYPE_ACCELEROMETER");
        		float X 	= event.values[0];
        		float Y 	= event.values[1];
        		float Z 	= event.values[2];
        		AccelX.setText("X : " + String.valueOf(X));
        		AccelY.setText("Y : " + String.valueOf(Y));
        		AccelZ.setText("Z : " + String.valueOf(Z));
				break;
			case Sensor.TYPE_ORIENTATION :
				MyCountingSensorUpdateRate.CountAs("TYPE_ORIENTATION");
        		float Z_Yaw 	= event.values[0];
        		float X_Pitch 	= event.values[1];
        		float Y_Roll 	= event.values[2];
        		OrienX.setText("X_Pitch : " + String.valueOf(X_Pitch));
        		OrienY.setText("Y_Roll : " + String.valueOf(Y_Roll));
        		OrienZ.setText("Z_Yaw : " + String.valueOf(Z_Yaw));
				break;
			}
        }
    }
    

}

送到測試著裝置安裝後,藉由 OTG 連接arduino 後,我們就可以點擊 [Start Counting] 按鈕,發送 arduino firmata connect 然後就可以看到 LED 明滅效果了

對了,arduino 端要先燒錄 StandardFirmata Example 喔!

我們明天見 :P


上一篇
x86 android 設備與外部硬體溝通研究 - Android USB -1 (18/30)
下一篇
x86 android 設備與外部硬體溝通研究 - Android USB -3 (PWM) (20/30)
系列文
x86 android 設備與外部硬體溝通研究30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言